<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
$a = getdate();
$b = date("Y-m-d",mktime(0, 0, 0, $a["mon"], $a["mday"], $a["year"]));
$data["LottoID_n"] = (empty($_data["LottoID"]))?$_data["LottoID"]:$_data["LottoID"];
$data["Bet_Type_n"] = (empty($Bet_Type))?$data["Bet_Type_n"]:$Bet_Type;
$data["PayupDate_x"][0] = (empty($_data["PayupDate_0"]))?date("Y-m-d"):$_data["PayupDate_0"];
$data["PayupDate_x"][1] = (empty($_data["PayupDate_1"]))?date("Y-m-d"):$_data["PayupDate_1"];

/*if($_data["Types"] != ""){
	$data["StockID_n"] = $_data["StockID"];
	$data["MainID_n"] = $_data["MainID"];
}*/
if($isLimit){
	$data["PayupDate_x"][0] = getPayupDateOfAccountSearchLimit($data["PayupDate_x"][0], $isSevenDay);
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$GLOBALS["errormessage"] = "Over search date range!!";
		ErrMsgOutputHTML("Error", "", 1);
	}
}else{
	if($data["PayupDate_x"][0] > $data["PayupDate_x"][1]){
		$tmp = $data["PayupDate_x"][1];
		$data["PayupDate_x"][1] = $data["PayupDate_x"][0];
		$data["PayupDate_x"][0] = $tmp;
	}
}
$strSQL = SelectSQL ($data, "`bet_BetList` USE INDEX (PayupDate)");

$n = "select StockID, MainID, COUNT(*) as totals, SUM(MemStake) as MemStake, SUM(SubIncome) as SubIncome, SUM(SubRateResult) as SubRateResult, StockRate, MainRate, SUM(StockRateResult) as StockRateResult, SUM(StockResult) as StockResult, SUM(MainRateResult) as MainRateResult, SUM(IF(MemResult = 0, 0, MemStake)*MemExchangeRate*StockRate) as realMemStake, SUM(MemStake*MemExchangeRate*StockRate*-1*(StockCommission - MainCommission)) as StockComm";
$strSQL = str_replace("select *", $n, $strSQL);
$strSQL1 = $strSQL;
$strSQL .=" and isPayOff = 0 and WagerType in (0,2,3) group by MainID order by StockID, MainID limit 0, 500";
$strSQL1 .=" and isPayOff = 0 and WagerType in (4) group by MainID order by StockID, MainID limit 0, 500";
//一般帳
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Stock Report Error";
	$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
//print "<!--".$strSQL."//-->";
$arrRows = array();
$arrUserID = array();
$arrAccount = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrRows[0][] = $Rows;
	$arrUserID[] = $Rows -> StockID;
	$arrUserID[] = $Rows -> MainID;
}
//向上補貨帳
if($strSQL1 != ""){
	if(!$RS = PMA_mysql_query($strSQL1, $$_Conn)){
		$GLOBALS["errormessage"] = "Stock Report Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}
	while($Rows = PMA_mysql_fetch_object($RS)){
		$CrealMemStake += $Rows -> realMemStake;
		$arrRows[1][] = $Rows;
		$arrUserID[] = $Rows -> StockID;
		$arrUserID[] = $Rows -> MainID;
	}
}

//取得使用者 Account
if(sizeof($arrUserID) > 0){
	$strSQL = "select ID, Account, Name from `bet_Member` where ID in (".join(",", $arrUserID).")";
	if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
		$GLOBALS["errormessage"] = "Find User Data Error";
		$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
		ErrMsgOutputHTML("Error", "", 1);
	}

	while($Rows = PMA_mysql_fetch_object($RS)){
		$arrAccount[$Rows -> ID] = ($_KSysParam[ReportIsShowName]==0)?$Rows -> Account:$Rows -> Account."<a style='color: #336600'>(".$Rows -> Name.")</a>";
	}
}
?>